/**----------------------------------------
 * @description 测试脚本
 * @author      lxc
 * @file        test.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */
const { ccclass, property } = cc._decorator;

import { BaseView } from "../base/view/BaseVIew";

@ccclass
export class test extends BaseView {

    @property(cc.Node)
    templateButton: cc.Node = null;

    @property(cc.EditBox)
    edit: cc.EditBox = null;

    rowNum = 8;

    _AllBtns = []

    protected onLoad(): void {
        super.onLoad()
        // this.setPrefix("test")
        
        this.templateButton.active = false;
    }

    registClickEvent(){
        let event = new cc.Component.EventHandler()
        event.target = this.node
        event.component = "templateButton"
        event.handler = "onclick"
        this.templateButton.getComponent(cc.Button).clickEvents = [event]
    }

    protected onEnable(): void {
        super.onEnable()
        this.buttonTemplate("JumpGame"          , this.JumpGame.bind(this));
        this.buttonTemplate("JumpGame2"          , this.JumpGame2.bind(this));
    }

    JumpGame(){
        this.printLog("click  event")
        
    }

    JumpGame2(){
        this.printLog("click222  event")
    }

    protected onDisable(): void {
        this.printLog("onDisable")
    }

    onclick() {
        this.printLog("onclick")
        this.printLog(this.edit.string);
        this.node.destroy()
    }

    downloadPng(){
        let imgUrl = "https://download.cocos.com/test-case/logo.png";
        gVar.Download.loadFileByURL(imgUrl,(savePath)=>{
            cc.assetManager.loadRemote(savePath, (err, tex : cc.Texture2D) => {
                let node = new cc.Node();
                let sprite = node.addComponent(cc.Sprite);
                sprite.spriteFrame = new cc.SpriteFrame(tex);
                sprite.node.active = true;
            });
        })
    }

    buttonTemplate(str : string , func : Function){
        let outLog = () => {
            cc.log(str,node.position);
        }
        func = func || outLog ;
        let node = cc.instantiate(this.templateButton);
        node.getChildByName("Label").getComponent(cc.Label).string = str;
        node.on('click',func , this, true);
        node.active = true;
        node.parent = this.templateButton.parent;
        this.updatePostion(node);
    }

    FirstPos = new cc.Vec2(-460,220);
    updatePostion(node : cc.Node){
        let index = -1;
        for(let i = 0 ; i<this._AllBtns.length ; i++){
            if (node == this._AllBtns[i]){
                index = i;
                break;
            }
        }
        if ( index == -1){
            this._AllBtns.push(node);
            index = this._AllBtns.length -1;
        }
        let rowNum = index % this.rowNum;
        let col = Math.floor(index / this.rowNum);
        let x = this.FirstPos.x + rowNum * (node.getContentSize().width +20);
        let y = this.FirstPos.y;
        if (col > 0 ){
            y = this.FirstPos.y - col * (node.getContentSize().height +20);
        }
        // cc.log(" x : " , x , " y : " , y );
        node.setPosition(x,y);
    }
}